const res = [];
let currentPage = 1;
const fileName = 'mook'
const iframe = document.createElement("iframe");
iframe.onload = () => {
  const iDom = iframe.contentWindow.document;
  // console.log('iDom',iDom)
  const list = iDom.querySelectorAll(".course-list li");
  list.forEach(item => {
    const imgSrc = item.querySelector('.img').style.backgroundImage.match(/\((.+)\)/)[1].replace('\"', 'https:').replace('\"', '')
    const title = item.querySelector('.title')?.textContent
    const numbers = item.querySelector('.numbers')?.textContent
    const price = item.querySelector('.price')?.textContent.match(/([0-9.]+)/)[1]
    res.push({ imgSrc, title, numbers, price })
  })

  // 启动下一页
  if (currentPage < 5) {
    currentPage++;
    iframe.src = `https://coding.imooc.com/?sort=0&unlearn=0&page=${currentPage}`;
  } else {
    downloadText(fileName + ".json", JSON.stringify(res));
  }
};
document.body.appendChild(iframe);
iframe.src = "https://coding.imooc.com/?sort=0&unlearn=0&page=1";

function downloadText(fileName, text) {
  const url = window.URL || window.webkitURL || window;
  const blob = new Blob([text]);
  const saveLink = document.createElement("a");
  saveLink.href = url.createObjectURL(blob);
  // 设置 download 属性
  saveLink.download = fileName;
  saveLink.click();
  url.revokeObjectURL(saveLink.href);
  saveLink?.remove()
}
